#ifndef DEMUX_H
#define DEMUX_H

#include <QObject>
#include <iostream>
#include <future>
#include <QDebug>
#include <QImage>
#include <QDateTime>


extern "C"{
#include "libavcodec/avcodec.h"
#include "libavfilter/avfilter.h"
#include "libavformat/avformat.h"
#include "libavutil/avutil.h"
#include "libavutil/ffversion.h"
#include "libswresample/swresample.h"
#include "libswscale/swscale.h"
#include "libpostproc/postprocess.h"
#include <libavutil/imgutils.h>
}


class Demux : public QObject
{
    Q_OBJECT
public:
    explicit Demux(QObject *parent = nullptr);

    void setUrl(const QString & url);
    void paly();
    void stop();
    void setPause();
    void setSpeed(const double &speed);
    void setPreviousFrame();
    void seekFrame(const bool &isNextFrame);

    bool init();

signals:
    void sendQImage(QImage);
    void sendTime(double currTime, double duration);

private:
    QString _url;
    bool _isStop;
    std::future<int> _video;
    bool isPaused = false;
    std::mutex pauseMutex;
    std::condition_variable pauseCondition;
    double _speed = 1.0;
    bool _isPreviousFrame = false;
    int64_t _currentFrameIndex = 0;
    double _totalDuration = 0;

    AVFormatContext *fmtCtx=NULL;
    const AVCodec *videoCodec=NULL;
    AVCodecContext  *videoCodecCtx=NULL;
    AVPacket        *pkt          = NULL;
    AVFrame         *yuvFrame     = NULL;
    AVFrame         *rgbFrame     = NULL;

    struct SwsContext *img_ctx=NULL;

    unsigned char *out_buffer=nullptr;

    int videoStreamIndex =-1;
    int numBytes = -1;
signals:

};

#endif // DEMUX_H
